package com.jjm2473.dict;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author liangbin.llb
 * @since 2020/4/1
 */
public class DictionaryImpl implements Dictionary {
    private static final String[] EMPTY = new String[0];
    private String[][] index = new String['z' - 'a' + 1][];
    public DictionaryImpl(Set<String> words) {
        Map<Integer, List<String>> collect = words.stream().collect(Collectors.groupingBy(s -> s.charAt(0) - 'a'));
        collect.forEach((k,v)->{
            index[k] = v.toArray(new String[0]);
        });
    }

    @Override
    public String[] lookup(char firstChar) {
        return Optional.ofNullable(index[firstChar - 'a']).orElse(EMPTY);
    }
}
